﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.ViewQuery;
using System.Text;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_PaperManager_ProjectPaperManager : PowerCreator.LMS.Web.Base.BasePage
{
    ViewQuery objViewQuery = new ViewQuery();
    const string SEARCH_ProjectList = "select TTrainProject.TrainproID,TTrainProject.TrainproName,(select Count(*) from TTrainClass where TTrainProject.TrainproID=TTrainClass.FKTrainclaTrainproID )as ClassCount,(select Count(*)from TProjectResource where ProresClass=2 and  TTrainProject.TrainproID=TProjectResource.FKProresTrainproID) as PaperCount from TTrainProject where 1=1 ";

    const string SEARCH_PROJECTPAPERLIST = "select  tb.FKProrestrainproID,tb.TrainclaID,tb.FKProresPaperID,UserPaperHistory.FKUPHistoryCourseID, tb.PaperName,tb.TrainclaName,"


+ "(select count(UPHistoryID) from UserPaperHistory where FKUPHistoryProjectID= tb.FKProrestrainproID and FKUPHistoryClassID=tb.TrainclaID and FKUPHistoryPaperID=tb.FKProresPaperID and   UPHistorySubmitTime<>'') as SubmitPaperCount,"


+ "(select count(UPHistoryID) from UserPaperHistory where FKUPHistoryProjectID= tb.FKProrestrainproID and FKUPHistoryClassID=tb.TrainclaID and FKUPHistoryPaperID=tb.FKProresPaperID and   UPHistoryReadState=0 ) as UnReadPaperCount, "

+ "(select count(UPHistoryID) from UserPaperHistory where FKUPHistoryProjectID= tb.FKProrestrainproID and FKUPHistoryClassID=tb.TrainclaID and FKUPHistoryPaperID=tb.FKProresPaperID and UPHistoryIsPass=1 ) as PassCount, "

+ "(select count(UPHistoryID) from UserPaperHistory where FKUPHistoryProjectID= tb.FKProrestrainproID and FKUPHistoryClassID=tb.TrainclaID and FKUPHistoryPaperID=tb.FKProresPaperID )as AllSumCount    "

//+ "((select count(UPHistoryID) from UserPaperHistory where FKUPHistoryProjectID= tb.FKProrestrainproID and FKUPHistoryClassID=tb.TrainclaID and FKUPHistoryPaperID=tb.FKProresPaperID and UPHistoryIsPass=1 ) "

//+ "/(select count(UPHistoryID) from UserPaperHistory where FKUPHistoryProjectID= tb.FKProrestrainproID and FKUPHistoryClassID=tb.TrainclaID and FKUPHistoryPaperID=tb.FKProresPaperID ) )as PassRatio "

 + "from UserPaperHistory inner join (   "

+ "select TProjectResource.FKProrestrainproID,TTrainClass.TrainclaID,TProjectResource.FKProresPaperID, Paper.PaperName,TTrainClass.TrainclaName from "

+ "TProjectResource left join Paper on Paper.PaperID=TProjectResource.FKProresPaperID  "

+ "left join TTrainClass on TTrainClass.FKTrainclaTrainproID=TProjectResource.FKProresTrainproID "

+ "where TProjectResource.ProresClass=2 and TProjectResource.FKProrestrainproID={0} )as tb on FKUPHistoryProjectID=tb.FKProrestrainproID "

+ "and FKUPHistoryClassID=tb.TrainclaID and FKUPHistoryPaperID=tb.FKProresPaperID ";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
            lbl_InfoTitle.Text = GetLanageValueByKey("msg_projectpaperManager_title", PowerCreator.LMS.Language.LanagetItemType.Page);
            lab_ProjectName.Text = GetLanageValueByKey("msg_projectpaperManager_prname", PowerCreator.LMS.Language.LanagetItemType.Page);
            btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        }
    }
    private string GetSearch()
    {
        StringBuilder search = new StringBuilder();
        search.Append(SEARCH_ProjectList);
        if (txtProjectName.Text.Trim() != "")
        {
            search.Append(" and TrainproName like '%" + txtProjectName.Text.Trim() + "%'");
        }
        return search.ToString();
    }

    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = LMSPageSize;
        AspNetPager1.RecordCount = objViewQuery.Count(search);
    }

    private void BinderRPT(string search, int pageIndex)
    {
        DataTable dt = objViewQuery.Query(search, LMSPageSize, pageIndex);
        rpt_ProjectList.DataSource = dt;
        rpt_ProjectList.DataBind();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        //ViewState["CourseID"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rpt_ProjectList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "btn_show")
        {
            for (int i = 0; i < rpt_ProjectList.Items.Count; i++)
            {
                Panel pl_ResourceList = (Panel)rpt_ProjectList.Items[i].FindControl("pl_ResourceList");
                pl_ResourceList.Visible = false;
            }
            Panel pl_ResourceList1 = (Panel)e.Item.FindControl("pl_ResourceList");
            pl_ResourceList1.Visible = true;
            Repeater rptResource = (Repeater)e.Item.FindControl("rpt_ResourceList");
            //课件类型的课程资源

            string search = string.Format(SEARCH_PROJECTPAPERLIST, e.CommandArgument.ToString());
            DataTable dt = objViewQuery.Query(search);
            dt.Columns.Add("PassRatio");
            dt.Columns.Add("Url");
            dt.Columns.Add("ProjectResourceID");
            dt.Columns.Add("CourseResourceID");

            PowerCreator.LMS.Web.Project.ProjectResource objProjectResource = new PowerCreator.LMS.Web.Project.ProjectResource();
            PowerCreator.LMS.Web.Course.Course objCourse = new PowerCreator.LMS.Web.Course.Course();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                #region 获取方案资源ID和课程资源ID
                if (dt.Rows[i]["FKUPHistoryCourseID"].ToString().Trim() == "0")//0:试卷，1：课程底下的试卷
                {
                    string search_ProjectResource = " ProresClass=2 and FKProresTrainproID=" + dt.Rows[i]["FKProrestrainproID"].ToString() + " and FKProresCourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString() + " and FKProresPaperID=" + dt.Rows[i]["FKProresPaperID"].ToString();
                    DataTable dt_ProjectResource = objProjectResource.QueryProjectResource(search_ProjectResource);
                    if (dt_ProjectResource.Rows.Count == 0)
                    {
                        dt.Rows[i]["ProjectResourceID"] = 0;
                    }
                    else
                    {
                        dt.Rows[i]["ProjectResourceID"] = dt_ProjectResource.Rows[0]["ProresID"].ToString();
                    }
                    dt.Rows[i]["CourseResourceID"] = "0";
                }
                else
                {
                    string search_ProjectResource = " ProresClass=1 and FKProresTrainproID=" + dt.Rows[i]["FKProrestrainproID"].ToString() + " and FKProresCourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString() + " and FKProresPaperID=" + dt.Rows[i]["FKProresPaperID"].ToString();
                    DataTable dt_ProjectResource = objProjectResource.QueryProjectResource(search_ProjectResource);

                    if (dt_ProjectResource.Rows.Count == 0)
                    {
                        dt.Rows[i]["ProjectResourceID"] = 0;
                    }
                    else
                    {
                        dt.Rows[i]["ProjectResourceID"] = dt_ProjectResource.Rows[0]["ProresID"].ToString();
                    }

                    string search_CourseResource = " fkCoursersResourceID=0 and CoursersClass=2 and FKCourseresCourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString() + " and FKCoursersPaperID=" + dt.Rows[i]["FKProresPaperID"].ToString();

                    DataTable dt_CourseResource = objCourse.QueryCourseResource(search_CourseResource);

                    if (dt_CourseResource.Rows.Count == 0)
                    {
                        dt.Rows[i]["CourseResourceID"] = 0;
                    }
                    else
                    {
                        //dt.Rows[i]["CourseResourceID"] = dt_ProjectResource.Rows[0]["CourseresID"].ToString();
                        dt.Rows[i]["CourseResourceID"] = dt_CourseResource.Rows[0]["CourseresID"].ToString();      //Skey 更改 2010-4-22 1037
                    }

                }
                #endregion
                dt.Rows[i]["Url"] = "PaperID=" + dt.Rows[i]["FKProresPaperID"].ToString() + "&ProjectID=" + dt.Rows[i]["FKProrestrainproID"].ToString() + "&ClassID=" + dt.Rows[i]["TrainclaID"].ToString() + "&CourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString() + "&ProjectResourceID=" + dt.Rows[i]["ProjectResourceID"].ToString() + "&CourseResourceID=" + dt.Rows[i]["CourseResourceID"].ToString() + "&PaperName=" + dt.Rows[i]["PaperName"].ToString() + "&TrainclaName=" + dt.Rows[i]["TrainclaName"].ToString();
              string  PassRatio= Convert.ToString(int.Parse(dt.Rows[i]["PassCount"].ToString())/int.Parse(dt.Rows[i]["AllSumCount"].ToString()));
              dt.Rows[i]["PassRatio"] = PassRatio+"%";
            }
            
            
          
            if (dt.Rows.Count != 0)
            {
                rptResource.DataSource = dt;
                rptResource.DataBind();
            }
            else
            {
                pl_ResourceList1.Visible = false;
            }
        }   
    }

    protected void rpt_ResourceList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "See")
        {
            string url = "ProjectUserPaperList.aspx?" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
            
        }
    }
}
